package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseAllottedStoreDetailMapper;
import com.kaili.basic.mapper.BaseAllottedStoreMapper;
import com.kaili.basic.pojo.dto.BaseAllottedStoreDetailAddDTO;
import com.kaili.basic.pojo.entity.BaseAllottedStoreDetailEntity;
import com.kaili.basic.pojo.entity.BaseAllottedStoreEntity;
import com.kaili.basic.pojo.entity.BaseStoreLocationEntity;
import com.kaili.basic.service.BaseAllottedStoreDetailService;
import com.kaili.basic.service.BaseAllottedStoreService;
import com.kaili.basic.service.BaseStoreLocationService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BaseAllottedStoreDetailServiceImpl extends ServiceImpl<BaseAllottedStoreDetailMapper, BaseAllottedStoreDetailEntity> implements BaseAllottedStoreDetailService {

    @Autowired
    private BaseStoreLocationService baseStoreLocationService;

    @Override
    public void insertBatch(List<BaseAllottedStoreDetailAddDTO> detailList, String orderNo) {
        List<BaseAllottedStoreDetailEntity> list=new ArrayList<>();
        //查询出目标库位的默认正式库位
        List<BaseStoreLocationEntity> locations = baseStoreLocationService.selectByMap(new HashMap<String, Object>() {{
            put("store_id", detailList.get(0).getDestStoreId());
            put("store_location_type", 211);
            put("default_location", 0);
        }});
        for(BaseAllottedStoreDetailAddDTO detailAdd:detailList){
            BaseAllottedStoreDetailEntity detail=new BaseAllottedStoreDetailEntity();
            BeanUtils.copyProperties(detailAdd,detail);
            detail.setOrderNo(orderNo);
            detail.setDestLocationId(locations.get(0).getId().longValue());
            detail.setCreateTime(new Date());
            detail.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            list.add(detail);
        }
        this.insertBatch(list);
    }
}
